package com.ourway;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.facebook.Session;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class FriendsFragment extends Fragment{
	
	String uri;
	String jsonString;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		View view = inflater.inflate(R.layout.fragment_friends, container, false);
		TabHost tabHost = (TabHost) view.findViewById(R.id.tabHost);
		tabHost.setup();

		TabSpec specSchool=tabHost.newTabSpec("Tab 1");
		specSchool.setContent(R.id.tab1);
		specSchool.setIndicator("School");

		TabSpec specWork=tabHost.newTabSpec("Tab 2");
		specWork.setIndicator("Work");
		specWork.setContent(R.id.tab2);

		TabSpec specFamily=tabHost.newTabSpec("Tab 3");
		specFamily.setIndicator("Family");
		specFamily.setContent(R.id.tab3);

		tabHost.addTab(specSchool);
		tabHost.addTab(specWork);
		tabHost.addTab(specFamily);

		return view;
	}
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		MainActivity activity = (MainActivity) getActivity();
		Session session = activity.ses;
		uri = "https://graph.facebook.com/me/friends?access_token="+ session.getAccessToken();
        setHasOptionsMenu(true);
        
        try {
        	executeHttpGet();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
        
        JSONObject jsonObj = null;
        JSONArray data = null;
		try {
			jsonObj = new JSONObject(jsonString);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        try {
			data = jsonObj.getJSONArray("data");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        for (int i = 0; i < data.length(); i++) {
			
		}
        
	}
	
	public void executeHttpGet() throws Exception{
		BufferedReader in = null;
		String page = "";
		
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet();
			request.setURI(new URI(uri));
			HttpResponse response = client.execute(request);
			in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while((line = in.readLine()) != null){
				sb.append(line + NL);				
			}
			in.close();
			jsonString = sb.toString();
			System.out.println(page);
		}
		finally{
			if(in != null){
				try{
					in.close();
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
		inflater.inflate(R.menu.fragment_friends, menu);
		//super.onCreateOptionsMenu(menu, inflater);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_seeonmap:
			
			return true;
		case R.id.menu_chat:
			
			return true;
		}
		return false;
	}

}
